package com.jingyuan.biz.server;

import com.alibaba.fastjson.JSON;
import com.jingyuan.biz.event.*;
import com.jingyuan.biz.manager.JNodeManager;
import com.jingyuan.common.entity.EventType;
import com.jingyuan.biz.manager.JNode;
import com.jingyuan.common.entity.packet.BasePacket;
import com.jingyuan.common.entity.packet.MessageType;
import com.jingyuan.common.entity.packet.RegistPacket;
import com.jingyuan.common.entity.packet.VotePacket;
import io.netty.channel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;

@Service
@ChannelHandler.Sharable
public class ServerEventHandler extends ChannelInboundHandlerAdapter {
    @Autowired
    private EventManager eventManager;

    @Autowired
    private JNodeManager nodeManager;
    /**
     * 有新连接建立时
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("有客户端已连接");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //super.channelRead(ctx, msg);
        BasePacket basePacket =  (BasePacket)msg;
        System.out.println("收到消息"+JSON.toJSONString(basePacket));
        InetSocketAddress socketAddress=(InetSocketAddress) ctx.channel().remoteAddress();
        String clientIP = socketAddress.getAddress().getHostAddress();
        //System.out.println("client IP is "+clientIP);
        BaseEvent baseEvent=null;
        int msgType=basePacket.getMsgType();
        if(msgType==MessageType.ONLINE_NOTIFY){
            //机器上线
            RegistPacket registPacket=(RegistPacket)basePacket;
            JNode jNode=new JNode();
            jNode.setChannel(ctx.channel());
            jNode.setAddress(registPacket.getAddress());
            jNode.setPort(registPacket.getPort());
            jNode.setLastKeepLiveTime(System.currentTimeMillis());
            RegistEvent registEvent=new RegistEvent();
            registEvent.setNode(jNode);
            registEvent.setEventType(EventType.ONLINE);
            baseEvent=registEvent;
        }else if(msgType==MessageType.HEARTBEAT){
            //收到心跳包
            HeartBeatEvent heartBeatEvent=new HeartBeatEvent();
            heartBeatEvent.setChannel(ctx.channel());
            heartBeatEvent.setAddress(clientIP);
            JNode node=nodeManager.getNodeByChannel(ctx.channel() );
            heartBeatEvent.setNode(node);
            baseEvent=heartBeatEvent;
        }else if(msgType==MessageType.OFFLINE_NOTIFY){
            //收到主动下线包，为了优雅下线
            OffLineEvent offLineEvent=new OffLineEvent();
            offLineEvent.setChannel(ctx.channel());
            offLineEvent.setAddress(clientIP);
            //主动下线
            offLineEvent.setActive(true);
            offLineEvent.setNode(nodeManager.getNodeByChannel(ctx.channel()));
            baseEvent=offLineEvent;
        }else if(msgType==MessageType.VOTE){
            VotePacket votePacket=(VotePacket)basePacket;
            VoteEvent voteEvent=new VoteEvent();
            voteEvent.setPhase(VoteEvent.VOTE_MSG_RESV);
            voteEvent.setNode(nodeManager.getNodeByChannel(ctx.channel()));
            voteEvent.setVoteValue(votePacket.getRandomNumber());
            baseEvent=voteEvent;
        }
        //分发事件
        eventManager.dispatch(baseEvent);
    }


}
